package org.jeecg.modules.wmhb.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.SysDepartModel;
import org.jeecg.modules.wmhb.entity.WmTask;
import org.jeecg.modules.wmhb.service.IWmTaskService;
import org.jeecg.modules.wmhb.util.WmhbUtil;
import org.jeecg.modules.wmhb.entity.WmTaskFlow;
import org.jeecg.modules.wmhb.service.IWmTaskFlowService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

/**
 * @Description: 任务流程时间
 * @Author: jeecg-boot
 * @Date: 2024-09-13
 * @Version: V1.0
 */
@Api(tags = "任务流程时间")
@RestController
@RequestMapping("/wmhb/wmTaskFlow")
@Slf4j
public class WmTaskFlowController extends JeecgController<WmTaskFlow, IWmTaskFlowService> {
    @Autowired
    private IWmTaskFlowService wmTaskFlowService;
    @Autowired
    ISysBaseAPI sysBaseApi;
    @Autowired
    private IWmTaskService wmTaskService;

    /**
     * 分页列表查询
     *
     * @param wmTaskFlow
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "任务流程时间-分页列表查询")
    @ApiOperation(value = "任务流程时间-分页列表查询", notes = "任务流程时间-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<WmTaskFlow>> queryPageList(WmTaskFlow wmTaskFlow,
                                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                   HttpServletRequest req) {
        QueryWrapper<WmTaskFlow> queryWrapper = QueryGenerator.initQueryWrapper(wmTaskFlow, req.getParameterMap());
        Page<WmTaskFlow> page = new Page<WmTaskFlow>(pageNo, pageSize);
        IPage<WmTaskFlow> pageList = wmTaskFlowService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param wmTaskFlow
     * @return
     */
    @AutoLog(value = "任务流程时间-添加")
    @ApiOperation(value = "任务流程时间-添加", notes = "任务流程时间-添加")
    @RequiresPermissions("wmhb:wm_task_flow:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody WmTaskFlow wmTaskFlow) {
        wmTaskFlowService.save(wmTaskFlow);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param wmTaskFlow
     * @return
     */
    @AutoLog(value = "任务流程时间-编辑")
    @ApiOperation(value = "任务流程时间-编辑", notes = "任务流程时间-编辑")
    @RequiresPermissions("wmhb:wm_task_flow:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody WmTaskFlow wmTaskFlow) {
        wmTaskFlowService.updateById(wmTaskFlow);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "任务流程时间-通过id删除")
    @ApiOperation(value = "任务流程时间-通过id删除", notes = "任务流程时间-通过id删除")
    @RequiresPermissions("wmhb:wm_task_flow:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        wmTaskFlowService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "任务流程时间-批量删除")
    @ApiOperation(value = "任务流程时间-批量删除", notes = "任务流程时间-批量删除")
    @RequiresPermissions("wmhb:wm_task_flow:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.wmTaskFlowService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "任务流程时间-通过id查询")
    @ApiOperation(value = "任务流程时间-通过id查询", notes = "任务流程时间-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<WmTaskFlow> queryById(@RequestParam(name = "id", required = true) String id) {
        WmTaskFlow wmTaskFlow = wmTaskFlowService.getById(id);
        if (wmTaskFlow == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(wmTaskFlow);
    }

    /**
     * 通过wmTaskId查询
     *
     * @param wmTaskId
     * @return
     */
    //@AutoLog(value = "任务流程时间-通过wmTaskId查询")
    @ApiOperation(value = "任务流程时间-通过wmTaskId查询", notes = "任务流程时间-通过wmTaskId查询")
    @GetMapping(value = "/queryByTaskId")
    public Result<List<WmTaskFlow>> queryByTaskId(@RequestParam(name = "wmTaskId", required = true) String wmTaskId) {
        List<WmTaskFlow> wmTaskFlowList = wmTaskFlowService.queryByTaskId(wmTaskId);
        for (WmTaskFlow wmTaskFlow : wmTaskFlowList) {
            SysDepartModel sysDepartModel = sysBaseApi.selectAllById(wmTaskFlow.getDepartId());
            WmTask wmTask = wmTaskService.getById(wmTaskFlow.getWmTaskId());
            wmTaskFlow.setWmTaskName(wmTask.getName());
            wmTaskFlow.setDepartName(sysDepartModel.getDepartName());
            wmTaskFlow.setUseTime(WmhbUtil.countDiffDay(wmTaskFlow.getFactBeginTime(), wmTaskFlow.getFactEndTime()));
        }
        return Result.OK(wmTaskFlowList);
    }
}
